const tencentcloud = require("tencentcloud-sdk-nodejs");

const jwt = require("jsonwebtoken");

module.exports = {
    sendSmS(tel, code){
        // Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
        const SmsClient = tencentcloud.sms.v20190711.Client;

        const clientConfig = {
        credential: {
            secretId: "AKID5oLbaPUAzKWYSCrZJyYvW0GOsChLi8LZ",
            secretKey: "KgeNq2MPziBdopgV9PVRQtBd2iRya6MT",
        },
        region: "",
        profile: {
            httpProfile: {
            endpoint: "sms.tencentcloudapi.com",
            },
        },
        };

        const client = new SmsClient(clientConfig);
        const params = {
            "PhoneNumberSet": [
                "+86" + tel
            ],
            "TemplateID": "881527",
            "Sign": "爱code爱生活",
            "TemplateParamSet": [
                code
            ],
            "SmsSdkAppid": "1400489828"
        };
        return client.SendSms(params);
    },
    verify(t){
        return new Promise((resolve,reject)=>{
            jwt.verify(t, "千锋H5，前端界的扛把子", (err, data)=>{
                if(err){
                    reject(err)
                }else{
                    resolve(data)
                }
            })
        })
    },
    sign(o, t){
        return jwt.sign(o, "千锋H5，前端界的扛把子", {
            expiresIn: t
        });
    }
}